package com.service.impl.user;

import com.core.dto.PageDTO;
import com.core.dto.WorkOverTimeDTO;
import com.core.model.lxf.WorkOverTime;
import com.dao.WorkOverTimeDao;
import com.service.user.WorkOverTimeService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2017/2/17
 * Time: 14:44
 * To change this template use Editor | File and Code Templates.
 */
@Service("workOverTimeService")
@Transactional
public class WorkOverTimeServiceImpl implements WorkOverTimeService {

    @Autowired
    private WorkOverTimeDao workOverTimeDao;

    @Override
    public boolean saveOrUpdateWorkOverTime(WorkOverTimeDTO workOverTimeDTO) {
        boolean result = true;
        try {
            WorkOverTime find = workOverTimeDao.findIsExistByGuid(WorkOverTime.class, workOverTimeDTO.getGuid());
            WorkOverTime workOverTime = workOverTimeDTO.toWorkOverTime(workOverTimeDTO);
            if (find == null) {
                workOverTimeDao.saveOrUpdate(workOverTime);
            } else {
                find.init(workOverTime);
                workOverTimeDao.saveOrUpdate(find);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Map<String, Object> findAllWorkOverTime(String currentPage) {
        Map<String, Object> map = new HashedMap();
        String totalPage = workOverTimeDao.findTotalPage(WorkOverTime.class);
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<WorkOverTime> workOverTimes = workOverTimeDao.findAll(WorkOverTime.class, currentPage);
        map.put("workOverTimeDTO", WorkOverTimeDTO.toDTOs(workOverTimes));
        map.put("currentPage", currentPage);
        map.put("totalPage", totalPage);
        return map;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public WorkOverTimeDTO findWorkOverTimeByGuid(String workOverTimeGuid) {
        WorkOverTime workOverTime = workOverTimeDao.findByGuid(WorkOverTime.class, workOverTimeGuid);
        return WorkOverTimeDTO.toDTO(workOverTime);
    }

    @Override
    public boolean delWorkOverTimeByGuid(String workOverTimeGuid) {
        boolean result = true;
        try {
            WorkOverTime workOverTime = workOverTimeDao.findIsExistByGuid(WorkOverTime.class, workOverTimeGuid);
            if (workOverTime != null) {
                workOverTimeDao.delObject(workOverTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

}
